﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Yunwei.BizLogic;
using Yunwei.Domain.Models;
using Yunwei.IBizLogic;

namespace Yunwei.Web.Areas.RRShop.Models
{
    public class FoodViewModel
    {
        IStaffService _staffService;
        ICategoryService _categoryService;
        public FoodViewModel()
        {
            _staffService = new StaffService();
            _categoryService = new CategoryService();
        }

        public int Id { get; set; }

        [Required]
        [StringLength(20, MinimumLength = 1, ErrorMessage = "{2}到{1}个字符")]
        [Display(Name = "名称")]
        public string Name { get; set; }

        [Required]
        [Display(Name = "菜系")]
        public short Cuisine_Id { get; set; }

        [Required]
        [Display(Name = "详述")]
        public string Description { get; set; }

        [Required]
        [StringLength(200, MinimumLength = 1, ErrorMessage = "{2}到{1}个字符")]
        [Display(Name = "图片")]
        public string Avatar { get; set; }

        [Required]
        [Display(Name = "价格")]
        public decimal Price { get; set; }

        private decimal discount = 10.0m;
        [Required]
        [Range(0,10,ErrorMessage="0到10之间")]
        [Display(Name = "折扣")]
        public decimal Discount
        {
            get { return discount; }
            set { discount = value; }
        }

        [Required]
        [Display(Name = "添加时间")]
        public System.DateTime CreateTime { get; set; }

        [Display(Name="简介")]
        [StringLength(20, MinimumLength = 1, ErrorMessage = "{2}到{1}个字符")]
        public string Brief { get; set; }

        [Required]
        [Display(Name = "厨师")]
        public int Chef_Id { get; set; }

        [Required]
        [Display(Name = "分类")]
        public int Category_Id { get; set; }

        [Required]
        [Display(Name = "餐馆")]
        public int Restaurant_Id { get; set; }

        private Staff _chef;
        public Staff Chef
        {
            get
            {
                if (_chef == null)
                    _chef = _staffService.Find(Chef_Id);

                return _chef;
            }
        }

        private Category _category;
        public Category Category
        {
            get
            {
                if(_category==null)
                {
                    _category = _categoryService.Find(this.Category_Id);
                }

                return _category;
            }
        }

        private Cuisine cuisine;
        public Cuisine Cuisine
        {
            get
            {
                if (cuisine == null)
                    cuisine = Cuisines.Find(c => c.Id == this.Cuisine_Id);
                return cuisine;
            }
        }

        private List<SelectListItem> _chefItems;
        public List<SelectListItem> ChefItems
        {
            get
            {
                if(_chefItems==null)
                {
                    _chefItems = new List<SelectListItem>();
                    var chefs = _staffService.FindList(100, "", 1, this.Restaurant_Id, 1);
                    foreach(var chef in chefs)
                    {
                        SelectListItem item = new SelectListItem()
                        {
                            Text = chef.Name,
                            Value = chef.Id + "",
                            Selected = this.Chef_Id == chef.Id
                        };

                        _chefItems.Add(item);
                    }
                }

                return _chefItems;
            }
        }

        private List<SelectListItem> categoryItems;
        public List<SelectListItem> CategoryItems
        {
            get
            {
                if(categoryItems==null)
                {
                    categoryItems = new List<SelectListItem>();

                    var categorys = _categoryService.FindList(100, "", this.Restaurant_Id, 1);
                    foreach(var category in categorys)
                    {
                        SelectListItem item = new SelectListItem()
                        {
                            Text = category.Name,
                            Value = category.Id + "",
                            Selected = category.Id == this.Category_Id
                        };
                        categoryItems.Add(item);
                    }
                }

                return categoryItems;
            }
        }

        private List<Cuisine> cuisines;
        public List<Cuisine> Cuisines
        {
            get
            {
                if (cuisines == null)
                {
                    cuisines = new List<Cuisine>();
                    cuisines.Add(new Cuisine() { Id = 1, Name = "川菜" });
                    cuisines.Add(new Cuisine() { Id = 2, Name = "鲁菜" });
                    cuisines.Add(new Cuisine() { Id = 3, Name = "粤菜" });
                    cuisines.Add(new Cuisine() { Id = 4, Name = "苏菜" });
                    cuisines.Add(new Cuisine() { Id = 5, Name = "浙菜" });
                    cuisines.Add(new Cuisine() { Id = 6, Name = "闽菜" });
                    cuisines.Add(new Cuisine() { Id = 7, Name = "湘菜" });
                    cuisines.Add(new Cuisine() { Id = 8, Name = "徽菜" });

                    cuisines.Add(new Cuisine() { Id = 11, Name = "潮州菜" });
                    cuisines.Add(new Cuisine() { Id = 12, Name = "东北菜" });
                    cuisines.Add(new Cuisine() { Id = 13, Name = "本帮菜" });
                    cuisines.Add(new Cuisine() { Id = 14, Name = "赣菜" });
                    cuisines.Add(new Cuisine() { Id = 15, Name = "鄂菜" });
                    cuisines.Add(new Cuisine() { Id = 16, Name = "京菜" });
                    cuisines.Add(new Cuisine() { Id = 17, Name = "津菜" });
                    cuisines.Add(new Cuisine() { Id = 18, Name = "冀菜" });
                    cuisines.Add(new Cuisine() { Id = 19, Name = "豫菜" });
                    cuisines.Add(new Cuisine() { Id = 20, Name = "客家菜" });
                    cuisines.Add(new Cuisine() { Id = 21, Name = "清真菜" });
                }

                return cuisines;
            }
        }

        private List<SelectListItem> cuisineItems;
        public List<SelectListItem> CuisineItems
        {
            get
            {
                if (cuisineItems == null)
                {
                    cuisineItems = new List<SelectListItem>();
                    foreach (var cuisine in Cuisines)
                    {
                        SelectListItem item = new SelectListItem()
                        {
                            Text = cuisine.Name,
                            Value = cuisine.Id + "",
                            Selected = cuisine.Id == this.Cuisine_Id
                        };
                        cuisineItems.Add(item);
                    }
                }
                return cuisineItems;
            }
        }

        private Food food;
        public Food Food
        {
            get
            {
                if (food == null)
                {
                    food = new Food()
                    {
                        Id = this.Id,
                        Name = this.Name,
                        Cuisine_Id = this.Cuisine_Id,
                        Description = this.Description,
                        Avatar = this.Avatar,
                        Price = this.Price,
                        Discount = this.Discount,
                        CreateTime = this.CreateTime,
                        Chef_Id = this.Chef_Id,
                        Category_Id = this.Category_Id,
                        Restaurant_Id = this.Restaurant_Id,
                        Brief = this.Brief
                    };
                }

                return food;
            }
        }

        public static FoodViewModel FromFood(Food food)
        {
            FoodViewModel fvm = new FoodViewModel()
            {
                Id = food.Id,
                Name = food.Name,
                Cuisine_Id = food.Cuisine_Id,
                Description = food.Description,
                Avatar = food.Avatar,
                Price = food.Price,
                Discount = food.Discount,
                CreateTime = food.CreateTime,
                Chef_Id = food.Chef_Id,
                Category_Id = food.Category_Id,
                Restaurant_Id = food.Restaurant_Id,
                Brief = food.Brief
            };
            return fvm;
        }
    }
}